<script setup>
  import { ref, watch } from 'vue';
  import { useRoute,useRouter } from "vue-router";
  
  const route = useRoute()
  const router = useRouter()
  
  const tabbarData = [
    {
      title:'首页',
      icon: 'home-o',
      path: '/home'
    },
    {
      title:'热点',
      icon: 'search',
      path: '/hotStop'
    },
    {
      title:'发帖',
      icon: 'plus',
      path: '/toPost',
      isSpecial: true // 标记为特殊处理
    },
    {
      title:'游戏库',
      icon: 'friends-o',
      path: '/gameLibrary'
    },
    {
      title:'我',
      icon: 'setting-o',
      path: '/profile'
    },
  ]
  const currentIndex = ref(0)
  
  // 监听路由变化，记录当前页面到 sessionStorage
  watch(() => route.path, (newPath) => {
    if (newPath !== '/toPost') {
      sessionStorage.setItem('lastPage', newPath)
    }
  }, { immediate: true })
  
  // 处理发帖按钮点击
  function handleTabClick(item, index) {
    if (item.isSpecial) {
      // 发帖按钮特殊处理，传递当前页面信息
      const currentPath = route.path
      router.push({
        path: '/toPost',
        query: { from: currentPath }
      })
    } else {
      // 其他按钮正常跳转
      router.push(item.path)
    }
  }
</script>
<template>
  <van-tabbar v-model="currentIndex">
    <van-tabbar-item v-for="(item, index) in tabbarData" 
                     :key="index"
                     :icon="item.icon"
                     @click="handleTabClick(item, index)">
      {{ item.title }}
    </van-tabbar-item>
  </van-tabbar>
</template>
<style lang="less" scoped>
</style>